package com.leadnews.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leadnews.dtos.Filedto;
import com.leadnews.mapper.MaterialFreeMapper;
import com.leadnews.mapper.WemediaMapper;
import com.leadnews.pojo.MaterialFree;
import com.leadnews.pojo.User;
import com.leadnews.pojo.Wemedia;
import com.leadnews.pojo.WemediaMaterial;
import com.leadnews.service.WemediaMaterialService;
import com.leadnews.mapper.WemediaMaterialMapper;
import com.leadnews.service.WemediaService;
import com.leadnews.utils.TokenFilter;
import com.leadnews.utils.oss.OssUtil;
import com.leadnews.vo.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author 自己
 * @description 针对表【wemedia_material】的数据库操作Service实现
 * @createDate 2023-01-08 19:14:32
 */
@Service
@Slf4j
public class WemediaMaterialServiceImpl extends ServiceImpl<WemediaMaterialMapper, WemediaMaterial>
        implements WemediaMaterialService{
    @Autowired
    TokenFilter tokenFilter;
    @Autowired
    MaterialFreeMapper materialFreeMapper;
    @Autowired
    OssUtil ossUtil;
    @Override
    public ResponseResult removepic(Filedto fileList, HttpServletRequest request) {
        User token = tokenFilter.getToken(request);
        ResponseResult result = new ResponseResult();
        if (token==null){
            result.setCode(300);
            result.setErrorMessage("no access ,please login");
            return result;
        }

        try {
            LambdaQueryWrapper<MaterialFree> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(MaterialFree::getName,fileList.getName())
                    .eq(MaterialFree::getSizeimage,fileList.getSize())
            .eq(MaterialFree::getUid,token.getId());
            List<MaterialFree> wemediaMaterials = materialFreeMapper.selectList(queryWrapper);
            if (wemediaMaterials!=null){
                ossUtil.remove(wemediaMaterials.get(0).getUrl());
                int i = materialFreeMapper.deleteById(wemediaMaterials.get(0).getId());
                if (i>0){
                    log.info("{}","删除素材成功");
                }
            }
            ossUtil.remove(wemediaMaterials.get(0).getUrl());
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

}




